package com.xmcowell.sportpacemaster.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

import com.xmcowell.sportpacemaster.R;
import com.xmcowell.sportpacemaster.config.Contents;
import com.xmcowell.sportpacemaster.config.Type;

import java.util.ArrayList;
import java.util.List;

/**
 * 绘制坐标轴
 * Created by Administrator on 2017/3/15.
 */

public class UserFragmentAxisView extends View {
    private int XPoint = 20;//原点X坐标
    private int YPoint = 0;//原点Y坐标
    private int XScale = 8;  //X轴每格刻度的长度
    private double YScale = 40;//Y轴每格刻度的长度
    private int max = 12;//Y轴的刻度个数
    private int width;//视图宽度
    private int height;//视图高度
    private int y_height = 5;

    private boolean flag = true;//当前界面，true为速度，false为坡度

    private int TEXTSIZE = 10;//默认字体大小
    private int textSize;//字体大小
    private int y_kedu;//刻度长度
    private int Y_KEDU = 5;//默认
    private int axis_y_more;//y轴顶部预留的长度
    private int AXIS_Y_MORE = 2;
    private Context ctx;


    public UserFragmentAxisView(Context context) {
        super(context);

    }

    public UserFragmentAxisView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.UserFragmentAxis);
        textSize = ta.getInteger(R.styleable.UserFragmentAxis_axis_textSize, TEXTSIZE);
        y_kedu = ta.getInteger(R.styleable.UserFragmentAxis_axis_y_kedu, Y_KEDU);
        axis_y_more = ta.getInteger(R.styleable.UserFragmentAxis_axis_y_more, AXIS_Y_MORE);
        ctx = context;
        ta.recycle();

    }

    public UserFragmentAxisView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (Contents.SPORT_TYPE.equals(Type.PM940)) {
            XPoint = 25;
        }
        width = getWidth();
        height = getHeight() - y_height;
        YScale = (height - axis_y_more) * 1.0 / (max) * 1.0;
        if (flag) {
            //速度
            if (Contents.isBicycle) {
                YScale = 14.8;
            } else {
                YScale = getYScaleValue();
            }

        } else {
            //坡度
            if (Contents.isBicycle) {
                YScale = 19.5;
            } else {
                YScale = getYScaleIncValue();
            }

        }


        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setAntiAlias(true);//去锯齿
        paint.setColor(Color.WHITE);
        //文字画笔
        Paint tvPaint = new Paint();
        tvPaint.setStyle(Paint.Style.STROKE);
        tvPaint.setAntiAlias(true);//去锯齿
        tvPaint.setColor(Color.WHITE);
        tvPaint.setTextSize(textSize);

        //画Y轴
        canvas.drawLine(XPoint, YPoint + height, XPoint, YPoint, paint);
        //添加Y轴的刻度与文字
        for (int i = 0; i < max + 1; i++) {
            Double y1 = YPoint + height - (i * YScale);
            float y = Float.parseFloat(y1.toString());
            if (i > 0) {
                canvas.drawLine(XPoint, y, XPoint + y_kedu, y, paint);
            }

            if (Contents.SPORT_TYPE.equals(Type.PM810)) {
                if (i < 10) {
                    canvas.drawText("  " + i, 0, y + 5, tvPaint);
                } else {
                    canvas.drawText(String.valueOf(i), 0, y + 5, tvPaint);
                }
            } else if (Contents.SPORT_TYPE.equals(Type.PM820) || Contents.SPORT_TYPE.equals(Type.PM930) || Contents.SPORT_TYPE.equals(Type.PM940)) {
                if (flag) {
                    if (i < 10) {
                        canvas.drawText("  " + i, 0, y + 5, tvPaint);
                    } else {
                        canvas.drawText(String.valueOf(i), 0, y + 5, tvPaint);
                    }
                } else {
                    if (i < 13 && i > 2) {
                        canvas.drawText("  " + String.valueOf(i - 3), 0, y + 5, tvPaint);
                    } else {
                        canvas.drawText(String.valueOf(i - 3), 0, y + 5, tvPaint);
                    }
                }

//                if (i == 3){
//                    canvas.drawLine(XPoint, YPoint + height, XPoint + width, YPoint + height, paint);
//                }
            }

        }
        //画X轴
        canvas.drawLine(XPoint, YPoint + height, XPoint + width, YPoint + height, paint);
    }

    /**
     * 画虚线
     *
     * @param canvas
     */
    private void drawX(Canvas canvas) {
        DashPathEffect pathEffect = new DashPathEffect(new float[]{1, 2}, 1);
        Paint paint = new Paint();
        paint.reset();
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(1);
        paint.setColor(Color.WHITE);
        paint.setAntiAlias(true);
        paint.setPathEffect(pathEffect);
        Path path = new Path();
        path.moveTo(50, 50);
        path.lineTo(50, 200);
        canvas.drawPath(path, paint);
    }

    /**
     * 设置Y轴最大值
     *
     * @param flag true为速度，false为坡度
     * @param max  最大值
     */
    public void setYLabel(boolean flag, int max) {
        if (flag) {
            this.max = max / 10;
        } else {
            this.max = max;
        }

        this.invalidate();
    }

    /**
     * 设置当前是速度还是坡度
     *
     * @param flag
     */
    public void setType(boolean flag) {
        this.flag = flag;
        this.invalidate();
    }

    /**
     * 根据不同的类型，设置间隔宽度
     *
     * @return
     */
    private double getYScaleValue() {
        double value = 0;
        if (Contents.SPORT_TYPE.equals(Type.PM810)) {
            //810 公制0-20，英制0-12
            if (Contents.getUnit(ctx)) {
                //公制
                value = 15.2;//17.2
            } else {
                //英制
                value = 24;//27
            }
        } else if (Contents.SPORT_TYPE.equals(Type.PM820)) {
            //820 公制0-20，英制0-12
            if (Contents.getUnit(ctx)) {
                //公制
                value = 15.2;
            } else {
                //英制
                value = 24;
            }
        } else if (Contents.SPORT_TYPE.equals(Type.PM930)) {
            //930 公制0-22，英制0-14
            if (Contents.getUnit(ctx)) {
                //公制
                if (Contents.isTTYS3) {
                    value = 11.5;
                } else {
                    value = 13;
                }
            } else {
                //英制
                if (Contents.isTTYS3) {
                    value = 18;
                } else {
                    value = 20.5;
                }
            }
        } else {
            //940 公制0-25.6，英制0-16
            if (Contents.getUnit(ctx)) {
                //公制
                if (Contents.isTTYS3) {
                    value = 13.1;
                } else {
                    value = 16;
                }
            } else {
                if (Contents.isTTYS3) {
                    value = 21;
                } else {
                    value = 25;
                }
            }

        }
        return value;
    }

    private double getYScaleIncValue() {
        double YScale = 0;
        if (Contents.SPORT_TYPE.equals(Type.PM810)) {
//            YScale = 21.5;
            YScale = 19.2;
            //PM810 0-15
        } else if (Contents.SPORT_TYPE.equals(Type.PM940)) {
//                PM940 -3~18
            if (Contents.isTTYS3) {
                YScale = 16;
            } else {
                YScale = 19;
            }
        } else if (Contents.SPORT_TYPE.equals(Type.PM930)) {
            //PM820 PM930 -3~15
            if (Contents.isTTYS3) {
                YScale = 14;
            } else {
                YScale = 16;
            }

        } else {
            //PM820 -3~15
            YScale = 16;
        }
        return YScale;
    }

}
